// 运算工具函数

/**
 * @description 减法运算，避免数据相减小数点后产生多位数和计算精度损失。
 * @param num1 减数
 * @param num2 被减数
 * @param decimalPlaces 保留小数位
 */
export const numSub = (num1: number, num2: number, decimalPlaces: number = 4): any => {
    let baseNum, baseNum1, baseNum2;
    let precision; // 精度
    try {
        baseNum1 = num1.toString().split('.')[1].length;
    } catch (e) {
        baseNum1 = 0;
    }
    try {
        baseNum2 = num2.toString().split('.')[1].length;
    } catch (e) {
        baseNum2 = 0;
    }
    baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
    precision = baseNum1 >= baseNum2 ? baseNum1 : baseNum2;
    return formatNumDecimals((num1 * baseNum - num2 * baseNum) / baseNum, decimalPlaces, true);
};
/**
 * @description 加法运算
 * @param arg1
 * @param arg2
 * @param decimalPlaces 保留小数位
 * @returns
 */
export const numAdd = (arg1: number, arg2: number, decimalPlaces: number = 4) => {
    if (arg1 == 0 && arg2 == 0) {
        return 0;
    }
    let r1, r2, m, c;
    try {
        r1 = arg1.toString().split('.')[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split('.')[1].length;
    } catch (e) {
        r2 = 0;
    }
    c = Math.abs(r1 - r2);
    m = Math.pow(10, Math.max(r1, r2));
    if (c > 0) {
        const cm = Math.pow(10, c);
        if (r1 > r2) {
            arg1 = Number(arg1.toString().replace('.', ''));
            arg2 = Number(arg2.toString().replace('.', '')) * cm;
        } else {
            arg1 = Number(arg1.toString().replace('.', '')) * cm;
            arg2 = Number(arg2.toString().replace('.', ''));
        }
    } else {
        arg1 = Number(arg1.toString().replace('.', ''));
        arg2 = Number(arg2.toString().replace('.', ''));
    }

    return formatNumDecimals((arg1 + arg2) / m, decimalPlaces, true);
};
/**
 * @description 乘法计算
 * @param arg1
 * @param arg2
 * @param decimalPlaces 保留小数位
 * @returns
 */
export const numMul = (arg1: number, arg2: number, decimalPlaces: number = 4) => {
    if (!arg1 || !arg2) {
        return 0.0;
    }
    let m = 0,
        s1 = arg1.toString(),
        s2 = arg2.toString();
    try {
        m += s1.split('.')[1].length;
    } catch (e) { }
    try {
        m += s2.split('.')[1].length;
    } catch (e) { }
    return formatNumDecimals((Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m), decimalPlaces, true);
};

/**
 * @description 除法运算，避免数据相除小数点后产生多位数和计算精度损失
 * @param num1 除数
 * @param num2 被除数
 * @param decimalPlaces 保留小数位
 */
export const numDiv = (num1: number, num2: number, decimalPlaces: number = 4): any => {
    let baseNum1 = 0,
        baseNum2 = 0;
    let baseNum3, baseNum4;
    try {
        baseNum1 = num1.toString().split('.')[1].length;
    } catch (e) {
        baseNum1 = 0;
    }
    try {
        baseNum2 = num2.toString().split('.')[1].length;
    } catch (e) {
        baseNum2 = 0;
    }
    baseNum3 = Number(num1.toString().replace('.', ''));
    baseNum4 = Number(num2.toString().replace('.', ''));
    const res = (baseNum3 / baseNum4) * Math.pow(10, baseNum2 - baseNum1);
    return res === Infinity ? 0 : formatNumDecimals(res, decimalPlaces, true);
};

/**
 * 小数位数超过设定值，保留的小数位数
 * @param num 数据源
 * @param  maxLength 保留的小数位数
 * @param forceFmt 是否强制格式化
 */
export const formatNumDecimals = (num: any, maxLength: number, forceFmt?: boolean) => {
    num = Number(num);
    const x = String(num).indexOf('.') + 1; // 小数点的位置
    const y = String(num).length - x; // 小数的位数
    if (forceFmt || (y > 0 && y > maxLength)) {
        num = toFixed(num, maxLength);
    }
    return skipNaN(num);
};

const skipNaN = (val: any) => {
    if (isNaN(Number(val))) {
        return val;
    } else {
        return Number(val);
    }
};

// 利用Round四舍五入
export const toFixed = (number: number, decimals: number) => {
    let selftDecimals = '1';
    for (let i = 0; i < decimals; i++) {
        selftDecimals += '0';
    }
    let m = 0,
        s1 = number.toString(),
        s2 = selftDecimals.toString();
    try {
        m += s1.split('.')[1].length;
    } catch (e) { }
    try {
        m += s2.split('.')[1].length;
    } catch (e) { }
    const num = (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
    return Math.round(num) / Number(selftDecimals);
};

// 获取n位随机数
export const getRandomCode = (length: number): string => {
    const charactors = '0123456789';
    let randomid = '';
    let i;
    for (let j = 1; j <= length; j++) {
        // tslint:disable-next-line: radix
        i = parseInt(10 * Math.random() + '');
        randomid = randomid + charactors.charAt(i);
    }

    return randomid;
};
